﻿using Application.MainModule.AddressManagement;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Domain.MainModule.Addresses;
using Domain.MainModule.Entities;
using System.Collections.Generic;
using Infraestructure.CrossCutting.IoC;

namespace Application.MainModule.Tests
{
    /// <summary>
    ///This is a test class for AddressManagementServiceTest and is intended
    ///to contain all AddressManagementServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AddressManagementServiceTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        /// <summary>
        ///A test for AddAddress
        ///</summary>
        [TestMethod()]
        public void AddAddress_Invoke_Test()
        {
            //Arrange
            IAddressManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IAddressManagementService>();
            Guid addressId = Guid.NewGuid();
            Address address = new Address()
            {
                AddressId = addressId,
                Street = "siempre viva",
                AddressType = new AddressType()
                {
                    Name = "Address Type",
                    TypeId = Guid.NewGuid(),
                    Addresses = new Domain.Core.Entities.TrackableCollection<Address>()
                },
                City = new City()
                {
                    Name = "Ciudad gotica",
                    CityId = Guid.NewGuid()
                }
                
            };

            //Act
            target.AddAddress(address);
            Address result = target.FindAddressById(addressId);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreEqual(address, result); 
        }

        /// <summary>
        ///A test for ChangeAddress
        ///</summary>
        [TestMethod()]
        public void ChangeAddress_Invoke_Test()
        {
            //Arrange
            IAddressManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IAddressManagementService>();
            Guid addressId = Guid.Parse("ABCDF000-1234-5678-9000-1234567890AB");
 
            //Act
            Address address = target.FindAddressById(addressId);
            string oldValue = address.Street;
            address.Street = "La calle del olvido";
            target.ChangeAddress(address);
            Address result = target.FindAddressById(addressId);

            //Assert
            Assert.IsNotNull(result);
            Assert.AreNotEqual(oldValue, result.Street);
        }

        /// <summary>
        ///A test for FindAddressByUserId
        ///</summary>
        [TestMethod()]
        public void FindAddressByUserId_Invoke_Test()
        {
            //Arrange
            IAddressManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IAddressManagementService>();
            Guid userId = Guid.Parse("ABCDF000-1234-5678-9000-1234567890CD");

            //Act
            List<Address> actual;
            actual = target.FindAddressByUserId(userId);

            //Assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(1, actual.Count);
            Assert.AreEqual(userId, actual[0].UserId);
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void RemoveAddress_Invoke_NullItemThrowNewArgumentNullException_Test()
        {
            //Arrange
            IAddressManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IAddressManagementService>();

            //Act
            target.RemoveAddress(null);
        }

        /// <summary>
        ///A test for RemoveAddress
        ///</summary>
        [TestMethod()]
        public void RemoveAddress_Invoke_Test()
        {
            //Arrange
            IAddressManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IAddressManagementService>();
            Address address = null;

            //Act
            address = target.FindAddressById(Guid.Parse("ABCDF000-1234-5678-9000-1234567890AB"));
            target.RemoveAddress(address);

            //Assert
            Address result = target.FindAddressById(Guid.Parse("ABCDF000-1234-5678-9000-1234567890AB"));
            Assert.IsNull(result);
            address = new Address()
                    {
                        AddressId = Guid.Parse("ABCDF000-1234-5678-9000-1234567890AB"),
                        Street = "Calle 13",
                        AddressType = new AddressType()
                        {
                            Name = "AT",
                            TypeId = Guid.NewGuid(),
                            Addresses = new Domain.Core.Entities.TrackableCollection<Address>()
                        },
                        City = new City()
                        {
                            Name = "Balcarce",
                            CityId = Guid.NewGuid()
                        },
                        UserId = Guid.Parse("ABCDF000-1234-5678-9000-1234567890CD")
                    };
            target.AddAddress(address);
        }

        /// <summary>
        ///A test for FindAddressById
        ///</summary>
        [TestMethod()]
        public void FindAddressById_Invoke_Test()
        {
            //Arrange
            IAddressManagementService target = IoCFactory.Instance.CurrentContainer.Resolve<IAddressManagementService>();
            Guid addressId = Guid.Parse("ABCDF000-1234-5678-9000-1234567890AB");

            //Act 
            Address address = target.FindAddressById(addressId);

            //Assert
            Assert.IsNotNull(address);
            Assert.AreEqual(addressId, address.AddressId);
        }
    }
}
